package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.PageQuery;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.PageVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/users")
//@AllArgsConstructor

//加final的加入构造函数
@RequiredArgsConstructor
@Api(tags = "用户管理")
public class UserController {
//构造函数注入
//    private IUserService userService;
//    public UserController(IUserService userService) {
//        this.userService = userService;
//    }

//注解注入
//    @Autowired
//    private IUserService userService;


    private final IUserService userService;
    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        User user =BeanUtil.copyProperties(userFormDTO,User.class);
        userService.save(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable Long id){
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable Long id){
//        User user=userService.getById(id);
//        return BeanUtil.copyProperties(user,UserVO.class);
        return userService.queryUserAndAddressById(id);
    }
    @ApiOperation("根据id批量查询用户")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id集合")  @RequestParam("ids") List<Long> ids){
//        List<User> users=userService.listByIds(ids);
//        return BeanUtil.copyToList(users,UserVO.class);
        return  userService.queryUserAndAddressByIds(ids);
    }

    @ApiOperation("扣减用户余额")
    @PutMapping("/{id}/deductiong/{money}")
    public void deductBalance(@ApiParam("用户id")  @PathVariable("id") Long id,
                              @ApiParam("扣减的金额")  @PathVariable("money") Integer money){
        userService.deductBalance(id,money);
    }


    @ApiOperation("根据复杂业务查询")
    @GetMapping("/list")
    public List<UserVO> queryUserByIds(UserQuery userQuery){
        List<User> users=userService.queryUserByIds(userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        return BeanUtil.copyToList(users,UserVO.class);
    }

//    @GetMapping("/page")
//    public PageVO<UserVO> queryUserByPage(PageQuery query){
//        return userService.queryUserByPage(query);
//    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public PageDTO<UserVO> queryUsersByPage(UserQuery query){
        return userService.queryUsersByPage(query);
    }

}
